<template>
  <div class="app-container">
    <h2 class="text-center">{{ paperData.title }}</h2>
    <el-card style="margin-top: 20px">
      <div v-for="item in paperData.quList" :key="item.quId" class="qu-content">
        <p>{{ item.sort + 1 }}.{{ item.content }}</p>
        <div v-if="item.quType === 1 || item.quType===3">
          <el-radio-group v-model="radioValues[item.quId]">
            <div v-for="an in item.answerList" :key="an.id">
              <el-radio :label="an.id">{{ an.content }}</el-radio>
            </div>
          </el-radio-group>

          <el-row :gutter="24">
            <el-col :span="12" style="color: #24da70">正确答案：{{ radioRights[item.quId] }}</el-col>

            <el-col
              v-if="!item.answered"
              :span="12"
              style="text-align: right; color: #ff0000;"
            >答题结果：未答</el-col>

            <el-col
              v-if="item.answered && !item.isRight"
              :span="12"
              style="text-align: right; color: #ff0000;"
            >答题结果：{{ myRadio[item.quId] }}</el-col>

            <el-col
              v-if="item.answered && item.isRight"
              :span="12"
              style="text-align: right; color: #24da70;"
            >答题结果：{{ myRadio[item.quId] }}</el-col>
          </el-row>
        </div>

        <div v-if="item.quType === 4">
          <el-row :gutter="24">
            <el-col :span="12">我的回答：{{ item.answer }}</el-col>
          </el-row>
        </div>

        <div v-if="item.quType === 2">
          <el-checkbox-group v-model="multiValues[item.quId]">
            <div v-for="an in item.answerList" :key="an.id">
              <el-checkbox :label="an.id">{{ an.content }}</el-checkbox>
            </div>
          </el-checkbox-group>

          <el-row :gutter="24">
            <el-col :span="12" style="color: #24da70">正确答案：{{ multiRights[item.quId].join(',') }}</el-col>

            <el-col
              v-if="!item.answered"
              :span="12"
              style="text-align: right; color: #ff0000;"
            >答题结果：未答</el-col>

            <el-col
              v-if="item.answered && !item.isRight"
              :span="12"
              style="text-align: right; color: #ff0000;"
            >答题结果：{{ myMulti[item.quId].join(',') }}</el-col>

            <el-col
              v-if="item.answered && item.isRight"
              :span="12"
              style="text-align: right; color: #24da70;"
            >答题结果：{{ myMulti[item.quId].join(',') }}</el-col>
          </el-row>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 试卷ID
      paperId: '',
      paperData: {
        quList: []
      },
      radioValues: {},
      multiValues: {},
      radioRights: {},
      multiRights: {},
      myRadio: {},
      myMulti: {}
    }
  },
  created () {
    const workId = this.$route.query.workId
    const userId = this.$route.query.userId
    if (typeof workId !== 'undefined') {
      this.paperId = workId
      this.fetchData(workId, userId)
    }
  },
  methods: {

    fetchData (workId, userId) {
      this.$http({
        url: this.$http.adornUrl('/workmanagement/work/getWorkResult'),
        method: 'get',
        params: this.$http.adornParams({
          'workId': workId,
          'userId': userId
        })
      }).then(({ data }) => {
        // 试卷内容
        this.paperData = data.result

        // 填充该题目的答案
        this.paperData.quList.forEach((item) => {
          let radioValue = ''
          let radioRight = ''
          let myRadio = ''
          const multiValue = []
          const multiRight = []
          const myMulti = []

          item.answerList.forEach((an) => {
            // 用户选定的
            if (an.checked) {
              if (item.quType === 1 || item.quType === 3) {
                radioValue = an.id
                myRadio = an.content
              } else {
                multiValue.push(an.id)
                myMulti.push(an.content)
              }
            }

            // 正确答案
            if (an.isRight) {
              if (item.quType === 1 || item.quType === 3) {
                radioRight = an.content
              } else {
                multiRight.push(an.content)
              }
            }
          })

          this.multiValues[item.quId] = multiValue
          this.radioValues[item.quId] = radioValue

          this.radioRights[item.quId] = radioRight
          this.multiRights[item.quId] = multiRight

          this.myRadio[item.quId] = myRadio
          this.myMulti[item.quId] = myMulti
        })

        console.log(this.multiValues)
        console.log(this.radioValues)
      })
    }
  }
}
</script>

<style scoped>
.qu-content {
  border-bottom: #eee 1px solid;
  padding-bottom: 10px;
}

.qu-content div {
  line-height: 30px;
}

.el-checkbox-group label,
.el-radio-group label {
  width: 100%;
}

.card-title {
  background: #eee;
  line-height: 35px;
  text-align: center;
  font-size: 14px;
}
.card-line {
  padding-left: 10px;
}
.card-line span {
  cursor: pointer;
  margin: 2px;
}
</style>

